package com.example.demo.controller;

//在controller层不要出现domain实体user

import com.alibaba.fastjson.JSONObject;
import com.example.demo.req.UserLoginReq;
import com.example.demo.req.UserQueryReq;
import com.example.demo.req.UserResetPasswordReq;
import com.example.demo.req.UserSaveReq;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.PageResp;
import com.example.demo.resp.UserLoginResp;
import com.example.demo.resp.UserQueryResp;
import com.example.demo.service.UserService;
import com.example.demo.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    private  static  final Logger LOG= (Logger) LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SnowFlake snowFlake;



        @GetMapping("/list")
        public CommonResp list(@Valid UserQueryReq req){
            CommonResp<PageResp<UserQueryResp>> resp=new CommonResp<>();
            PageResp<UserQueryResp> list=userService.list(req);
            resp.setContent(list);
            return resp;
        }

        /**
         * @RequestBody
         *   对应jsaon方式的post请求
         *
         *   ptrp  form表单就不用注解
         * */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp=new CommonResp<>();
       userService.save(req);

        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp=new CommonResp<>();
        userService.delete(id);

        return resp;
    }
    @PostMapping("/reset-password")
    public CommonResp resentPassword(@Valid @RequestBody UserResetPasswordReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp=new CommonResp<>();
        userService.resetPassword(req);
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp=new CommonResp<>();
        UserLoginResp userLoginResp=userService.login(req);

        Long token =snowFlake.nextId();
         LOG.info("生成单击登录token：{}，并放入resis中",token);


        userLoginResp.setToken(token.toString());
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResp) ,3600*24, TimeUnit.SECONDS);

        resp.setContent(userLoginResp);
       return resp;
    }

    @GetMapping("/logout/token")
    public CommonResp logout(@PathVariable String token){
        CommonResp resp=new CommonResp<>();
        redisTemplate.delete(token);
        LOG.info("从redis中删除了token：{}",token);

        return resp;
    }

}
